<?php
namespace Mobile\Controller;

class OrderController extends MobileController {
    /**
    * 获取订单列表
    * 
    */
    public function index($p=1,$limit=10){
        if(empty($this->uid)){
            $this->err('用户未登录!');
        }
        $modelOrder = M('Order');
        $where['status'] = array('egt',0);
        $where['uid'] = $this->uid;
        $count = $modelOrder->where($where)->count();
        $resOrder = $modelOrder->where($where)->page($p,$limit)->select();
        $modelProduct = M('Product');
        $modelObdDayLog = M('ObdDayLog');
        foreach($resOrder as &$row){
            $product_id = $row['product_id'];
            $resProduct = $modelProduct->cache(true)->where("product_id=$product_id")->find();
            if($resProduct){
                $row['product_name'] = $resProduct['product_name'];
                $tableName = $resProduct['order_table'];
                $model = M($tableName);
                $where['corder_id'] = $row['targetid'];
                $resCic = $model->where($where)->find();
                if($resCic){
                    $dh = array();
                    $status = $resCic['status'];
                    if($status >= 1){//已支付的话，取保单号
                        $modelNo = M("{$tableName}PolicyList");
                        $resDh = $modelNo->where($where)->select();
                        foreach($resDh as $row1){
                            if($row1['business_code']=='11'){
                                $dh['syx'] = $row1['c_policy_no'];
                            }else{
                                $dh['jqx'] = $row1['c_policy_no'];
                            }
                        }
                    }else{//未支付的话取投保单号
                        $modelNo = M("{$tableName}Dh");
                        $resDh = $modelNo->where($where)->select();
                        foreach($resDh as $row1){
                            if($row1['business_code']=='11'){
                                $dh['syx'] = $row1['c_proposal_no'];
                            }else{
                                $dh['jqx'] = $row1['c_proposal_no'];
                            }
                        }
                    }
                    if(empty($dh)) $dh = null;
                    $row['policyno'] = $dh;
                }
            }
            if(!empty($row['obdid'])){
                unset($where);
                $where['obdid'] = $row['obdid'];
                $where['pdate'] = array('between',array($row['start_date'],$row['end_date']));
                $where['car_status'] = 1;
                $row['run_num'] = $modelObdDayLog->where($where)->count();
                $where['car_status'] = 0;
                $row['stop_num'] = $modelObdDayLog->where($where)->count();
            }
            unset($row['targetid']);
            unset($row['uid']);
        }
        
        $ret = array(
            'count'=>$count,
            'data'=>$resOrder,
        );
        
        $this->succ($ret);
    }
    
    /**
    * 订单详情
    * 
    * @param mixed $id
    */
    public function detail($id){
        if(empty($this->uid)){
            $this->err('用户未登录!');
        }
        $modelOrder = M('Order');
        $map['order_id'] = $id;
        $map['uid'] = $this->uid;
        $res = $modelOrder->where($map)->find();
        if($res){//验证下是否是该用户的保单 
            $product_id = $res['product_id'];
            $modelProduct = M('Product');
            $resProduct = $modelProduct->cache(true)->where("product_id=$product_id")->find();
            if($resProduct){
                $product_name = $resProduct['product_name'];
                $tableName = $resProduct['order_table'];
                
                $model = M($tableName);
                $where['corder_id'] = $res['targetid'];
                $resCic = $model->where($where)->find();
                if($resCic){
                    $dtFirst = new \DateTime($resCic['c_fst_reg_dte']);
                    $dtStart = new \DateTime($resCic['t_insrnc_bgn_tm']);
                    $dtDiff = $dtStart->diff($dtFirst);
                    $modelDetail = M("{$tableName}Detail");
                    $resCicDetail = $modelDetail->where($where)->select();
                    $dh = array();
                    $status = $resCic['status'];
                    if($status >= 1){//已支付的话，取保单号
                        $modelNo = M("{$tableName}PolicyList");
                        $resDh = $modelNo->where($where)->select();
                        foreach($resDh as $row1){
                            if($row1['business_code']=='11'){
                                $dh['syx'] = $row1['c_policy_no'];
                            }else{
                                $dh['jqx'] = $row1['c_policy_no'];
                            }
                        }
                    }else{//未支付的话取投保单号
                        $modelNo = M("{$tableName}Dh");
                        $resDh = $modelNo->where($where)->select();
                        foreach($resDh as $row1){
                             if($row1['business_code']=='11'){
                                $dh['syx'] = $row1['c_proposal_no'];
                            }else{
                                $dh['jqx'] = $row1['c_proposal_no'];
                            }
                        }
                    }
                    if(empty($dh)) $dh = null;
                    $resCic['detail'] = $resCicDetail;
                    $resCic['dh'] = $dh;
                    
                    $this->succ($resCic);
                }
            }
        }
        $this->err('无效的订单ID');
    }
}